import Axios from 'axios'

const state = {
    tableData: [],      //存放表格数据
    tabStatus: [1],    // 1 == 计量单位组  2 == 计量单位
    rowId: null,
    multipleSelection: [],
    selectRowId: []
}

const mutations = {
    updateTableData(state, arr) {     //更新表格数据
        if(Object.prototype.toString.call(arr) !== '[object Array]') return;
        let tableData = state.tableData;
        tableData.length = 0;
        arr.length > 0 ? tableData.splice(0, 1, ...arr) : tableData.splice(0);
    },
    updateTabStatus(state, n) {
        state.tabStatus.splice(0, 1, n);
    },
    updateRowId(state, id) {
        state.rowId = id ? id : null;
    },
    multipleSelection(state, arr) {
        state.multipleSelection.length = 0;
        state.selectRowId.length = 0;
        if(arr.length > 0) {
            arr.forEach(item => {
                state.multipleSelection.push(item);
                state.selectRowId.push(item['id']);
            });
        } else {
            state.multipleSelection.splice(0);
            state.selectRowId.splice(0);
        }
    }
}

const actions = {
    getUnitGroupTableData({commit}) {         //获取计量单位组表格数据
        Axios.get('measureUnit/selectGroup').then(res => {
            if(res.data.code == 20001) {
                if(res.data.data) {
                    commit('updateTableData', res.data.data);
                }
            }
        });
    },
    getUnitTableData({commit}) {            //获取计量单位表格数据
        Axios.get('measureUnit/selectUnit').then(res => {
            if(res.data.code == 20001) {
                if(res.data.data) {
                    commit('updateTableData', res.data.data);
                }
            }
        });
    }
}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}